package com.example.another.bottomnavigationdemo1;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.another.bottomnavigationdemo1.fragment.FirstFragment;
import com.example.another.bottomnavigationdemo1.fragment.FiveFragment;
import com.example.another.bottomnavigationdemo1.fragment.FourFragment;
import com.example.another.bottomnavigationdemo1.fragment.SecondFragment;
import com.example.another.bottomnavigationdemo1.fragment.ThirdFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 参考博客：https://blog.csdn.net/qq_23205911/article/details/73430979
 * 需要要导入design库
 */

public class FiveActivity extends AppCompatActivity {
    private List<Fragment> mFragmentList = new ArrayList<>();
    private TabLayout mTabLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_five);
        initViews();
    }

    private void initViews() {

        initFragments();
        showFragment(0);
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.personal_information_selected).setText("menu1"));
        mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.history_unselected).setText("menu2"));
        mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.search_unselected).setText("menu3"));
        mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.identify_unselected).setText("menu4"));
        mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.data_analysis_unselected).setText("menu5"));
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                initTabIcon();
                showFragment(tab.getPosition());
                switch (tab.getPosition()) {
                    case 0:
                        mTabLayout.getTabAt(0).setIcon(R.drawable.personal_information_selected);
                        break;
                    case 1:
                        mTabLayout.getTabAt(1).setIcon(R.drawable.history_selected);
                        break;
                    case 2:
                        mTabLayout.getTabAt(2).setIcon(R.drawable.search_selected);
                        break;
                    case 3:
                        mTabLayout.getTabAt(3).setIcon(R.drawable.identify_selected);
                        break;
                    case 4:
                        mTabLayout.getTabAt(4).setIcon(R.drawable.data_analysis_selected);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void initFragments() {
        mFragmentList.add(new FirstFragment());
        mFragmentList.add(new SecondFragment());
        mFragmentList.add(new ThirdFragment());
        mFragmentList.add(new FourFragment());
        mFragmentList.add(new FiveFragment());
    }

    private void initTabIcon() {
        mTabLayout.getTabAt(0).setIcon(R.drawable.personal_information_unselected);
        mTabLayout.getTabAt(1).setIcon(R.drawable.history_unselected);
        mTabLayout.getTabAt(2).setIcon(R.drawable.search_unselected);
        mTabLayout.getTabAt(3).setIcon(R.drawable.identify_unselected);
        mTabLayout.getTabAt(4).setIcon(R.drawable.data_analysis_unselected);
    }

    private void showFragment(int position) {
        Fragment fragment = mFragmentList.get(position);
        switch (position) {
            case 0:
                if (fragment == null) {
                    fragment = new FirstFragment();
                }
                break;
            case 1:
                if (fragment == null) {
                    fragment = new SecondFragment();
                }
                break;
            case 2:
                if (fragment == null) {
                    fragment = new ThirdFragment();
                }
                break;
            case 3:
                if (fragment == null) {
                    fragment = new FourFragment();
                }
                break;
            case 4:
                if (fragment == null) {
                    fragment = new FiveFragment();
                }
                break;
            default:
                break;
        }
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container5, fragment).commit();
        }
    }
}
